package com.gome.ocean.service.authority.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.gome.ocean.common.exception.UserServiceException;
import com.gome.ocean.dao.authority.UserDAO;
import com.gome.ocean.dao.model.authority.User;
import com.gome.ocean.service.authority.UserService;

/**
 * Created by liuqingxia on 2016/2/19.
 */
@Service
@Transactional("transactionManager")
public class UserServiceImpl implements UserService {

    private final static Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDAO             userDAO;

    @Override
    public boolean insertUser(User user) throws UserServiceException {
        if (!isQualifiedUser(user)) {
            throw new UserServiceException("用户属性未设置完备，请检查后重新尝试");
        }
        if (findUser(user.getEmail()) != null) {
            throw new UserServiceException("该用户已经注册过");
        }
        boolean result = false;
        try {
            result = userDAO.insertUser(user) > 0;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return result;
    }

    @Override
    public boolean removeUser(String email) throws UserServiceException {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("email不能为空");
        }
        if (findUser(email) == null) {
            throw new UserServiceException("该用户还没有注册");
        }
        boolean result = false;
        try {
            result = userDAO.removeUserByEmail(email) > 0;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return result;
    }

    @Override
    public boolean removeUser(Long id) throws UserServiceException {
        if (null == id) {
            throw new IllegalArgumentException("id不能为空");
        }
        if (findUserById(id) == null) {
            throw new UserServiceException("该用户还没有注册");
        }
        boolean result = false;
        try {
            result = userDAO.removeUserById(id) > 0;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return result;
    }

    @Override
    public boolean updateUser(User user) throws UserServiceException {
        if (!isQualifiedUser(user)) {
            throw new UserServiceException("用户属性未设置完备，请检查后重新尝试");
        }
        if (findUser(user.getEmail()) == null) {
            throw new UserServiceException("该用户还没有注册");
        }
        boolean result = false;
        try {
            result = userDAO.updateUser(user) > 0;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return result;
    }

    @Override
    public Page<User> findAllUser(int pageNum, int pageSize) throws UserServiceException {
        Page<User> page = null;
        try {
            page = (Page) userDAO.findAllUsers(new RowBounds(pageNum, pageSize));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return page;
    }

    @Override
    public User findUser(String email) throws UserServiceException {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("email不能为空");
        }
        User user = null;
        try {
            user = userDAO.findUserByEmail(email);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return user;
    }

    @Override
    public User findUserById(Long id) throws UserServiceException {
        if (null == id) {
            throw new IllegalArgumentException("id不能为空");
        }
        User user = null;
        try {
            user = userDAO.findUserById(id);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new UserServiceException(e.toString());
        }
        return user;
    }

    private boolean isQualifiedUser(User user) {
        if (null == user) {
            return false;
        }
        String email = user.getEmail();
        if (StringUtils.isBlank(email)) {
            return false;
        }
        String pwd = user.getPassword();
        if (StringUtils.isBlank(pwd)) {
            return false;
        }
        return true;
    }
}
